using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;
using FiltronASP.Helpers.Constants;

namespace FiltronASP.Models.DAO.WIX
{
    public class WixFilterDAO : Abstracts.AbstractGenericDAO<WixFilter, System.Nullable<int>>,
        Interfaces.WIX.IWixFilterDAO
    {
        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "filter_id";
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie silnika.
        /// </summary>
        /// <param name="engineId">silnik</param>
        /// <returns>rekordy o zadanym silniku lub null, gdy nie ma</returns>
        public IQueryable<WixFilter> FindByEngine(int engineId)
        {
            if (engineId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixFilter> filters = this._cmsEntitiesDataContext.WixFilters.Where(
                    filt => this._cmsEntitiesDataContext.WixFilterEngines.Where(fien =>
                        fien.engine_id.Equals(engineId)).Select(fe => fe.filter_id).Contains(filt.filter_id)
                        );
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie silnika i modelu.
        /// </summary>
        /// <param name="engineId">silnik</param>
        /// <param name="modelId">model</param>
        /// <returns>rekordy o zadanym silniku i modelu lub null, gdy nie ma</returns>
        public IQueryable<WixFilter> FindByEngineAndModel(int engineId, int modelId)
        {
            if (engineId.Equals(0))
            {
                return null;
            }   //if
            if (modelId.Equals(0))
            {
                return null;
            }
            try
            {
                IQueryable<WixFilter> filters = this._cmsEntitiesDataContext.WixFilters.Where(
                    filt => this._cmsEntitiesDataContext.WixFilterEngines.Where(fien =>
                        fien.engine_id.Equals(engineId) && 
                        fien.model_id.HasValue &&
                        fien.model_id.Value.Equals(modelId)).Select(fe => fe.filter_id).Contains(filt.filter_id)
                        );
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie typu.
        /// </summary>
        /// <param name="typeId">typ filtru</param>
        /// <returns>rekordy o zadanym typie lub null, gdy nie ma</returns>
        public IQueryable<WixFilter> FindByType(int typeId)
        {
            if (typeId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixFilter> filters = this._cmsEntitiesDataContext.WixFilters.Where(
                    filt => filt.filter_type_id.Equals(typeId)
                        );
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie numeru Filtra.
        /// </summary>
        /// <param name="filterNumber">numer(nazwa) Filtra</param>
        /// <returns>Filtr o podanej przez user'a nazwie.</returns>
        public IQueryable<WixFilter> FindByFilterNumber(string filterNumber)
        {
            if (filterNumber == "")
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixFilter> filters = this._cmsEntitiesDataContext.WixFilters.Where(
                    filt => filt.filter_number_trim.ToLower().Contains(filterNumber.ToLower())
                        );
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public IQueryable<WixFilter> FindByFilterNumberEquals(string filterNumber)
        {
            if (filterNumber == "")
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixFilter> filters = this._cmsEntitiesDataContext.WixFilters.Where(
                    filt => filt.filter_number_trim.ToLower().Equals(filterNumber.ToLower())
                        );
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public IQueryable<WixFilter> FindByFilterNumber(string filterNumber, Constants.TSearchInclusionMode searchMode)
        {
            if (filterNumber == "")
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixFilter> filters = null;
                switch (searchMode)
                {
                    case Constants.TSearchInclusionMode.simBoth:
                        filters = this._cmsEntitiesDataContext.WixFilters.Where(
                            filt => filt.filter_number_trim.ToLower().Contains(filterNumber.ToLower())
                                ).OrderBy(filt => filt.filter_number_trim);
                        break;
                    case Constants.TSearchInclusionMode.simExact:
                        filters = this._cmsEntitiesDataContext.WixFilters.Where(
                            filt => filt.filter_number_trim.ToLower().Equals(filterNumber.ToLower())
                                ).OrderBy(filt => filt.filter_number_trim);
                        break;
                    case Constants.TSearchInclusionMode.simLeft:
                        filters = this._cmsEntitiesDataContext.WixFilters.Where(
                            filt => filt.filter_number_trim.ToLower().StartsWith(filterNumber.ToLower())
                                ).OrderBy(filt => filt.filter_number_trim);
                        break;
                    case Constants.TSearchInclusionMode.simRight:
                        filters = this._cmsEntitiesDataContext.WixFilters.Where(
                            filt => filt.filter_number_trim.ToLower().EndsWith(filterNumber.ToLower())
                                ).OrderBy(filt => filt.filter_number_trim);
                        break;
                }   //switch
                if (filters == null)
                {
                    return null;
                }   //if
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public IQueryable<WixFilter> FindByFilterNumber(string filterNumber, Constants.TSearchInclusionMode searchMode, int limit)
        {
            if (filterNumber == "")
            {
                return null;
            }   //if
            try
            {
                if (limit <= 0)
                {
                    throw new Exception("Non-positive limit value");
                }   //if
                IQueryable<WixFilter> filters = null;
                switch (searchMode)
                {
                    case Constants.TSearchInclusionMode.simBoth:
                        filters = this._cmsEntitiesDataContext.WixFilters.Where(
                            filt => filt.filter_number_trim.ToLower().Contains(filterNumber.ToLower())
                                ).OrderBy(filt => filt.filter_number_trim).Take(limit);
                        break;
                    case Constants.TSearchInclusionMode.simExact:
                        filters = this._cmsEntitiesDataContext.WixFilters.Where(
                            filt => filt.filter_number_trim.ToLower().Equals(filterNumber.ToLower())
                                ).OrderBy(filt => filt.filter_number_trim).Take(limit);
                        break;
                    case Constants.TSearchInclusionMode.simLeft:
                        filters = this._cmsEntitiesDataContext.WixFilters.Where(
                            filt => filt.filter_number_trim.ToLower().StartsWith(filterNumber.ToLower())
                                ).OrderBy(filt => filt.filter_number_trim).Take(limit);
                        break;
                    case Constants.TSearchInclusionMode.simRight:
                        filters = this._cmsEntitiesDataContext.WixFilters.Where(
                            filt => filt.filter_number_trim.ToLower().EndsWith(filterNumber.ToLower())
                                ).OrderBy(filt => filt.filter_number_trim).Take(limit);
                        break;
                }   //switch
                if (filters == null)
                {
                    return null;
                }   //if
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie wymiarow.
        /// </summary>
        /// <param name="height">wysokosc</param>
        /// <param name="width">szerokosc</param>
        /// <param name="length">dlugosc</param>
        /// <param name="outsideDiaTop">zewnetrzna gorna srednica</param>
        /// <param name="outsideDiaBottom">zewnetrzna dolna srednica</param>
        /// <param name="insideDiaTop">wewnetrzna gorna srednica</param>
        /// <param name="insideDiaBottom">wewnetrzna dolna srednica</param>
        /// <returns>rekordy o zadanych wymiarach lub null, gdy nie ma</returns>
        public IQueryable<WixFilter> FindByDimensions(int? height, int? width, int? length, int? outsideDiaTop,
            int? outsideDiaBottom, int? insideDiaTop, int? insideDiaBottom)
        {
            return null;
        }

        
    }
}
